package com.mall.common.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;

import com.mall.common.bean.NavigationBean;

public class NavigationHelper {

	private static List<NavigationBean> navList;
	private static long lastModify;
	
	static {
		readFile();
	}
	
	@SuppressWarnings("unchecked")
	public static void readFile() {
		try {
			File file = new File(AppContext.getAppRealPath("WEB-INF/classes/navigation.xml"));
			lastModify = file.lastModified();
			navList = new ArrayList<NavigationBean>();
			Document document = (new SAXReader()).read(file);
//			//先解析变量
//			Map<String, String> propMap = new HashMap<String, String>();
//			List<org.dom4j.Element> properties = document.selectNodes("/project/properties/*");
//			for (Iterator<org.dom4j.Element> iterator = properties.iterator(); iterator.hasNext();){
//				org.dom4j.Element propertie = iterator.next();
//				propMap.put(propertie.getName(), propertie.getText());
//			}
//			
			List<org.dom4j.Element> list = document.selectNodes("/project/Attributes/attribute");
			for (Iterator<org.dom4j.Element> iterator = list.iterator(); iterator.hasNext();){
				org.dom4j.Element element1 = iterator.next();
				String id = element1.attributeValue("id");
				String name = element1.attributeValue("name");
				String attrName = element1.attributeValue("attrName");
				String target = element1.attributeValue("target");
				String icon = element1.attributeValue("icon");
				String href = element1.attributeValue("href");
				String clazz = element1.attributeValue("clazz");

				NavigationBean navigation = new NavigationBean();
				navigation.setId(id);
				navigation.setName(name);
				navigation.setAttrName(attrName);
				navigation.setClazz(clazz);
				navigation.setTarget(target);;
				navigation.setIcon(icon);
				navigation.setHref(href);
//				navigation.setHref(FreemarkerUtils.process(href, propMap));
				
				navList.add(navigation);
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}
	
	public static List<NavigationBean> getNavList() {
		File file = new File(AppContext.getAppRealPath("WEB-INF/classes/navigation.xml"));
		long curLastModify = file.lastModified();
		if(lastModify == curLastModify) {
			return navList;
		} else {
			readFile();
			return navList;
		}
	}
	
	public static NavigationBean getNavigation(String id){
		if(StringUtils.isEmpty(id)){
			return null;
		}
		for(NavigationBean bean : getNavList()){
			if(id.equals(bean.getId())) {
				return bean;
			}
		}
		return null;
	}
	
	public static List<NavigationBean> getByAttrName(String attrName){
		if(StringUtils.isEmpty(attrName)){
			return null;
		}
		List<NavigationBean> navList2 = new ArrayList<NavigationBean>();
		for(NavigationBean bean : getNavList()){
			if(attrName.equals(bean.getAttrName())) {
				navList2.add(bean);
			}
		}
		return navList2;
	}
}
